#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    main_widget = new QWidget();
     serial_config_layout = new QGridLayout();
     main_grid_interface = new QGridLayout();
     meter_time_layout = new QGridLayout();

     label_serial_port =new QLabel("端口");
     label_serial_baud = new QLabel("波特率");
     label_serial_parity = new QLabel("校验位");
     label_serial_data = new QLabel("数据位");
     label_serial_stop = new QLabel("停止位");
     commbox_serial_port  = new QComboBox();
     commbox_serial_baud = new QComboBox();
     commbox_serial_parity = new QComboBox();
     commbox_serial_data = new QComboBox();
     commbox_serial_stop = new QComboBox();

     pushButton_open_port =new QPushButton("Open");
     pushButton_close_port=new QPushButton("Close");
     pushButton_clearLogOut = new QPushButton("Clear");
     pushButton_LogAnalyse  = new QPushButton("Log_A");
     showAsciiShow    = new QCheckBox(tr("解析报文") );
     showAsciiShow->setChecked(FALSE);

     meter_time = new QLabel();   //添加电表时间。
     meter_run_time = new QLabel("0h0min0sec");
     QFrame_line = new QFrame();
     QFrame_line->setFixedHeight(10);
     QFrame_line->setAutoFillBackground(1);

     commbox_serial_port->addItem("COM1");
        commbox_serial_port->addItem("COM2");
        commbox_serial_port->addItem("COM3");
        commbox_serial_port->addItem("COM4");
        commbox_serial_port->addItem("COM5");
        commbox_serial_port->addItem("COM6");
        commbox_serial_port->addItem("COM7");
        commbox_serial_port->addItem("COM8");
        commbox_serial_port->addItem("COM9");
        commbox_serial_port->addItem("COM10");
        commbox_serial_port->addItem("COM11");
        commbox_serial_port->addItem("COM12");
        commbox_serial_port->addItem("COM13");
        commbox_serial_port->addItem("COM14");
        commbox_serial_port->addItem("COM15");
        commbox_serial_baud->addItem("9600");
        commbox_serial_baud->addItem("2400");
        commbox_serial_baud->addItem("115200");
        commbox_serial_parity->addItem("E");
        commbox_serial_parity->addItem("O");
        commbox_serial_parity->addItem("N");
        commbox_serial_data->addItem("8");
        commbox_serial_stop->addItem("1");
        serial_config_layout->addWidget(label_serial_port,0,0);
        serial_config_layout->addWidget(label_serial_baud,1,0);
        serial_config_layout->addWidget(label_serial_parity,2,0);
        serial_config_layout->addWidget(label_serial_data,3,0);
        serial_config_layout->addWidget(label_serial_stop,4,0);

        serial_config_layout->addWidget(commbox_serial_port,0,1);
        serial_config_layout->addWidget(commbox_serial_baud,1,1);
        serial_config_layout->addWidget(commbox_serial_parity,2,1);
        serial_config_layout->addWidget(commbox_serial_data,3,1);
        serial_config_layout->addWidget(commbox_serial_stop,4,1);

        serial_config_layout->addWidget(pushButton_open_port,5,0);
        serial_config_layout->addWidget(pushButton_close_port,5,1);

        serial_config_layout->addWidget(pushButton_clearLogOut,6,0);
        serial_config_layout->addWidget(showAsciiShow ,7,0);  //添加报文log 解析打印
 //        serial_config_layout->addWidget(log_analyselable,7,0);
//        serial_config_layout->addWidget(pushButton_showSQL,8,1);  //添加报文log 解析打印
//        serial_config_layout->addWidget(sql_show,8,0);
        serial_config_layout->addWidget(QFrame_line, 8, 0);
       // serial_config_layout->addWidget(meter_time,9,0);//虚拟电表时间，会自己增加，然后每5S会和数据库对一下时，如果修改过，那么用新的，否则用旧的继续累加。
       //  serial_config_layout->setRowStretch(9,1);  //在第10行上加上弹簧
        textshow = new QTextBrowser();
        textshowAnalyse = new QTextBrowser();


        //network 控件
        network_ip_label = new QLabel("IP");
        network_ip = new QLineEdit("172.20.45.35");
        network_ip->setFixedWidth(125);
        network_port_label = new QLabel("PORT");
        network_port = new QLineEdit("6400");
        network_port->setFixedWidth(125);
        network_connect = new QPushButton("connect");
        network_close = new QPushButton("close");;
        network_close->setEnabled(FALSE);
        network_connect_repeat  = new QCheckBox(tr("重复连接") );
        network_connect_repeat->setChecked(TRUE);
        socketConnectState = new QLabel(tr("等待连接"));
        network_config_layout = new QGridLayout();
        network_config_layout->addWidget(network_ip_label,0,0);
        network_config_layout->addWidget(network_ip, 0, 1);
        network_config_layout->addWidget(network_port_label, 1, 0);
        network_config_layout->addWidget(network_port, 1, 1);
        network_config_layout->addWidget(network_connect, 2, 0);
        network_config_layout->addWidget(network_close, 2, 1);
        network_config_layout->addWidget(network_connect_repeat,3,0);
         network_config_layout->addWidget(socketConnectState,3,1);
        network_config_layout->setRowStretch(4, 1);  //在第10行上加上弹簧
       //  network_config_layout->setColumnStretch(2, 0);  //在第10行上加上弹簧
        serial_rx_cnt = new QLabel();
        serial_tx_cnt = new QLabel();

        meter_time_layout->addWidget(meter_time,0,0);
       // meter_time_layout->setColumnStretch(0,1);
        meter_time_layout->addWidget(meter_run_time,0,1);
        meter_time_layout->addWidget(serial_rx_cnt, 0, 2);  //添加收发个数显示
        meter_time_layout->addWidget(serial_tx_cnt, 0, 3);

        main_grid_interface->addLayout(serial_config_layout,0,0);
        main_grid_interface->addLayout(network_config_layout, 1, 0);
        main_grid_interface->addWidget(textshow,0,1,1,1);
        main_grid_interface->addWidget(textshowAnalyse,1,1,1,1);
        main_grid_interface->setColumnStretch(1, 1);   //设置这里列的伸缩系数为1，其他为0；
        main_grid_interface->setRowStretch(0, 1);   //设置这里列的伸缩系数为1，其他为0；
        main_grid_interface->setRowStretch(1, 1);   //设置这里列的伸缩系数为1，其他为0；
        main_grid_interface->addLayout(meter_time_layout,2,1);
        main_widget->setLayout(main_grid_interface);

        setCentralWidget(main_widget);//加上这一句。

        menu_add();
        //添加信号与槽
        //定义串口
        serialObj = new QSerialPort();
        connect(pushButton_open_port,SIGNAL(clicked()),this,SLOT(serial_open_slot()));
        connect(pushButton_close_port,SIGNAL(clicked()),this,SLOT(serial_close_slot()));
        connect(pushButton_clearLogOut,SIGNAL(clicked()),this,SLOT(clear_log_out()));
        connect(pushButton_LogAnalyse,SIGNAL(clicked()),this,SLOT(open_log_analyse()));
       // connect(pushButton_showSQL,SIGNAL(clicked()),this,SLOT(open_sql_analyse()));
        //添加网络侦听接口
        connect(network_connect, SIGNAL(clicked()), this, SLOT(open_network_slot()));
        connect(network_close, SIGNAL(clicked()), this, SLOT(close_network_slot()));


        //初始化TCP客户端
        tcpClient = new QTcpSocket(this);
        tcpClient->abort();

        connect(tcpClient, SIGNAL(readyRead()), this, SLOT(slot_netreadData()) );
        connect(tcpClient, SIGNAL(connected()), this, SLOT(slot_connected()) );
         connect(tcpClient, SIGNAL(disconnected()), this, SLOT(slot_disconnected()) );
        //connect(tcpClient, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(slot_error(QAbstractSocket::SocketError)));


    timerid1 = startTimer(10);
    timerid2 = startTimer(1000*5);

}

MainWindow::~MainWindow()
{
}
void MainWindow::serial_open_slot()
{
    qDebug()<<"open clicked";
    bool open_res;

    //1-1 设置串口号。
    serialObj->setPortName(commbox_serial_port->currentText());
    //1-2设置波特率
    switch (commbox_serial_baud->currentIndex()) {
        case 0:
            serialObj->setBaudRate(QSerialPort::Baud9600);
            break;
         case 1:
            serialObj->setBaudRate(QSerialPort::Baud2400);
           break;
         case 2:
            serialObj->setBaudRate(QSerialPort::Baud115200);
          break;
     }
    //1-3设置数据位数
    serialObj->setDataBits(QSerialPort::Data8);
    //1-4设置校验位
    serialObj->setParity(QSerialPort::EvenParity);
    //1-5设置停止位
    serialObj->setStopBits(QSerialPort::OneStop);
    //1-6设置流控制 无控制流
    serialObj->setFlowControl(QSerialPort::NoFlowControl);
    //1-7那就是用读写方式打开串口了
   open_res =  serialObj->open(QIODevice::ReadWrite);
    // 2 如果打开成功，那么设置变灰，按钮变灰，打开失败那么弹出窗口提示失败
    if(open_res == true)
    {
        pushButton_open_port->setEnabled(false);
        commbox_serial_port->setEnabled(false);
        commbox_serial_baud->setEnabled(false);
        commbox_serial_parity->setEnabled(false);
        commbox_serial_data->setEnabled(false);
        commbox_serial_stop->setEnabled(false);
        pushButton_close_port->setEnabled(true);
        connect(serialObj,SIGNAL(readyRead()),this,SLOT(on_serial_read_slot()));
//        //初始化对应串口数据结构
//        serial_data_pool.head =0;
//        serial_data_pool.tail =0;
//        serial_data_pool.length =0;
//        QDir dir(QDir::currentPath()+"/log");
//        if(!dir.exists()){
//            bool ismkdir = dir.mkdir(QDir::currentPath()+"/log");
//            if(!ismkdir)
//                qDebug() << "Create path fail" << endl;
//            else
//                qDebug() << "Create fullpath success" << endl;
//        }
//        else{
//            qDebug() << "fullpath exist" << endl;
//        }
//        file_frame = new QFile(".\\log\\log"+QDateTime::currentDateTime().toString("yyyy_MM_dd_hh_mm_ss")+".txt");
//        if(!file_frame->open(QIODevice::WriteOnly|QIODevice::Text)){
//            qDebug()<<"文件打开失败";
//        }
        //向文件中写入两行字符串
        //file_frame->write("C语言中文网\n");
       // file_frame->write("http://c.biancheng.net");
        //关闭文件
       // file_frame->close();
    }
    else
    {
       QMessageBox::about(NULL, "About", "打开串口失败");
    }
//    gSystemInfo.serial_rx_cnt = 0;
//    gSystemInfo.serial_tx_cnt = 0;
}
void MainWindow::serial_close_slot()
{
    qDebug()<<"close clicked";
     serialObj ->close();
     pushButton_open_port->setEnabled(true);
     commbox_serial_port->setEnabled(true);
     commbox_serial_baud->setEnabled(true);
     commbox_serial_parity->setEnabled(true);
     commbox_serial_data->setEnabled(true);
     commbox_serial_stop->setEnabled(true);
     pushButton_close_port->setEnabled(false);
}
//50ms定时器到了。
void MainWindow::timerEvent(QTimerEvent *e)
 {
     if(e->timerId()==timerid1)
     {
         if(showAsciiShow->checkState() ==Qt::Checked)
         {
             if((serialIntCnt++>5) &&(serial_channel.receive_len_raw !=0))
             {
                serialIntCnt = 0;
             }
             else
             {
                 return ;
             }
             mutexUart.lock();
             QDateTime current_date_time =QDateTime::currentDateTime();
             QString current_date =current_date_time.toString("yyyy.MM.dd hh:mm:ss.zzz");
             QString  trans;
             trans.clear();
             trans.append(current_date);
             trans.append(" ");
              for(int i=0;i<serial_channel.receive_len_raw;i++)
              {
                //  trans.append(QString::number(static_cast<uchar>(res[i]), 16));
                  trans.append(QString("%1").arg(static_cast<uchar>(serial_channel.recevice_frame[i]), 2, 16, QLatin1Char('0')));
                  trans.append(' ');
              }
              textshow->append(trans);
              trans.clear();
              trans.append(current_date);
              trans.append(" ");
              for(int i=0;i<serial_channel.receive_len_raw;i++)
              {
                  if(serial_channel.recevice_frame[i] == 0xA ||serial_channel.recevice_frame[i] == 0xD)
                  {
                      trans.append(" ");
                      continue;
                  }
                  trans.append(serial_channel.recevice_frame[i]);
              }
             //这里学习使用QByteArray 转 char* 传统方式data()和size()函数
             serial_channel.receive_len_raw =0;
             mutexUart.unlock();
              textshowAnalyse->append(trans);
        }
         else   //对串口数据进行读取，需要加锁处理
         {
            mutexUart.lock();
            int idx=0;
            INT8U rx_data;
            while(idx <serial_channel.receive_len_raw)
            {
                rx_data = serial_channel.recevice_frame[idx];
                if(serial_channel.receive_state == APP_FRAME_RECV_HEADER)
                {
                        //报文头接收状态

                        //接收的字符压入报文头中
                        memcpy(serial_channel.receive_head,serial_channel.receive_head+1,RECE_FRAME_HEAD-1);
                        serial_channel.receive_head[RECE_FRAME_HEAD-1] = rx_data;

                        //检查是否已经接收到报头
                        protocol376.protocol_service_check_header(&serial_channel);
                        if(protocoloop.check_frame_header_gb_oop((INT8U*)serial_channel.receive_head))  //OOP的领出来处理
                        {
                            memcpy(serial_channel.recevice_frame_waite,serial_channel.receive_head,RECE_FRAME_HEAD);
                                serial_channel.receive_state = APP_FRAME_RECV_FRAME;
                                serial_channel.receive_len = RECE_FRAME_HEAD;
                        }
                        //判断645报文
                        if(((INT8U)serial_channel.receive_head[0] == 0x68)&&((INT8U)serial_channel.receive_head[7] == 0x68))
                        {
                                memcpy(serial_channel.recevice_frame_waite,serial_channel.receive_head,RECE_FRAME_HEAD);
                                serial_channel.receive_state = APP_FRAME_RECV_FRAME;
                                serial_channel.receive_len = RECE_FRAME_HEAD;  //645报文长度
                                memset(serial_channel.receive_head,0,sizeof(serial_channel.receive_head));
                        }
                    }
                    else
                    {
                        //报文体接收状态

//                         //加入报文
                         serial_channel.recevice_frame_waite[serial_channel.receive_len ++] = rx_data;

                         int res = protocol645Imp.isProtocol645((INT8U*)serial_channel.recevice_frame_waite,serial_channel.receive_len);
                         if(res == FALSE)
                         {
                             //检查报文接收是否完成...
                            res =  protocol376.protocol_service_check_frame(&serial_channel);
                            if(res==FALSE) //接续判断OOP协议。
                            {
                                res =protocoloop.check_frame_body_gb_oop((INT8U*)serial_channel.recevice_frame_waite,serial_channel.receive_len);
//                                qDebug()<<"oop receive normal"<<res<<serial_channel.receive_len;
                            }
                         }
                        if(res)   //这里把接收的报文打印出来。
                        {
                            serial_channel.receive_state = APP_FRAME_RECV_HEADER;
                            QDateTime current_date_time =QDateTime::currentDateTime();
                            QString current_date =current_date_time.toString("yyyy.MM.dd hh:mm:ss.zzz");
                            QString  trans;
                            trans.clear();
                            trans.append(current_date);
                            trans.append(" <==");
                            for(int i=0;i<serial_channel.receive_len;i++)
                            {
                              //  trans.append(QString::number(static_cast<uchar>(res[i]), 16));
                                trans.append(QString("%1").arg(static_cast<uchar>(serial_channel.recevice_frame_waite[i]), 2, 16, QLatin1Char('0')));
                                trans.append(' ');
                            }
                             textshow->append(trans);
                        }
                        if(  1==res)  //这里进行显示判断。
                          {
                                emit needToShowSignal();
                         }
                        else if( 2==res)  //需要判断确认或者否认
                        {
                                if((serial_channel.recevice_frame_waite[12] ==0)&&
                                     (serial_channel.recevice_frame_waite[14] ==0)&&
                                        (serial_channel.recevice_frame_waite[15] ==0))
                                {
                                    if((serial_channel.recevice_frame_waite[16] ==1)&&
                                         (serial_channel.recevice_frame_waite[17] ==0))
                                    {
                                            QMessageBox::information(this,  "QMessageBox::information()","All Confirm",QMessageBox::Ok);
                                    }
                                    else if  ((serial_channel.recevice_frame_waite[16] ==2)&&
                                                 (serial_channel.recevice_frame_waite[17] ==0))
                                    {
                                          QMessageBox::information(this,  "QMessageBox::information()","All Deny",QMessageBox::Ok);
                                     }
                                }
                        }
                        else   //报文检验失败
                        {

                        }
                    }
                 idx ++;
            }
            serial_channel.receive_len_raw =0;
            mutexUart.unlock();
         }
     }
     else if(e->timerId() ==timerid2)   //需要connect 进行重复执行。
     {
            if((connectIsValid ==TRUE) && network_connect_repeat->isChecked()  )
            {
                if(tcpClient->state() ==QAbstractSocket::ConnectedState)
                {

                }
                else
                {
                    if(tcpClient->state() !=QAbstractSocket::ConnectingState)   //不是在连接中。
                    {
                        socketConnectState->setText(tr("连接中"));
                        tcpClient->connectToHost(network_ip->text(), network_port->text().toInt(),QIODevice::ReadWrite,QAbstractSocket::IPv4Protocol);
                       // ui->btnOpen->setEnabled(false);
                        if( !tcpClient->waitForConnected(500) ) //只等待4秒
                        {
                            tcpClient->disconnectFromHost();
                    //        ui->btnOpen->setText(tr("连接"));
                    //        ui->btnOpen->setEnabled(true);
//                               QMessageBox::warning(this,
//                                tr("注意！"),
//                                tcpClient->errorString(),
//                                QMessageBox::Ok ,
//                                QMessageBox::Ok) ;
                        }
                    }
                }
            }
     }

 }
void MainWindow::on_serial_read_slot() //串口读槽。
{

     QByteArray res;
     serialIntCnt=0;
     res = serialObj->readAll();
     char *buf = res.data();
     int len = res.size();
     mutexUart.lock();
     for(int idx=0;idx <len;idx++)
     {
         if(serial_channel.receive_len_raw >=RECE_FRAME_LEN)
         {
             mutexUart.unlock();
             return;
         }
         serial_channel.recevice_frame[serial_channel.receive_len_raw] = *(buf+idx);
        serial_channel.receive_len_raw++;

     }
     mutexUart.unlock();
}
void MainWindow::on_serial_write_slot()  //串口写槽。
{
    if(serialObj->isOpen())
    {
        qDebug()<< "first send";
        QByteArray ba = QByteArray::fromRawData( serial_channel.response_frame, serial_channel.response_len);
    //    if (gSystemInfo.channelType == 0xA0)
    //    {
    //        client->write(ba);
    //    }
    //    else
        {
            serialObj->write(ba);
        }
        QDateTime current_date_time =QDateTime::currentDateTime();
        QString current_date =current_date_time.toString("yyyy.MM.dd hh:mm:ss.zzz");
        QString  trans;
        trans.clear();
        trans.append(current_date);
        trans.append(" ==>");
        for(int i=0;i<serial_channel.response_len;i++)
        {
          //  trans.append(QString::number(static_cast<uchar>(res[i]), 16));
            trans.append(QString("%1").arg(static_cast<uchar>(serial_channel.response_frame[i]), 2, 16, QLatin1Char('0')));
            trans.append(' ');
        }
         textshow->append(trans);
    }
}
void MainWindow::show_Log_slot()
{

}
void  MainWindow::clear_log_out()  // 清除 log 展示。
{
    textshow->clear();
    textshowAnalyse->clear();
}
void MainWindow:: open_log_analyse()  // 控制 log 展示。
{

}
void  MainWindow::open_sql_analyse()  // 清除 log 展示。
{

}
void MainWindow:: update_meter_timer_slot()
{

}
//menu 点击后的槽
void  MainWindow::showAbout_slot(QAction *action)
{

}
void  MainWindow::show_Operator_slot(QAction *action)
{
    if(action ==action_network)
    {
        qDebug()<<"newwork is ok";
//        network = new network_page();
//        network->show();
    }
    else if(action ==action_sendTest)   //发送报文测试。
    {
            sendtestpage1 = new sendtestpage();
            sendtestpage1->show();
            sendtestpage1->resize(500,100);
            connect(sendtestpage1, SIGNAL(sendframeSignal()), this, SLOT(on_serial_write_slot()));

    }
    else if(action ==actionParamSetRead)  //打开参数设置与查询页面。
    {
        paramsetread = new ParamSetAndRead();
        paramsetread->setWindowModality(Qt::ApplicationModal);
        paramsetread->setAttribute(Qt::WA_ShowModal,true);
        paramsetread->show();
        connect(paramsetread, SIGNAL(sendframeSignal_param_page()), this, SLOT(on_serial_write_slot()));
        connect(this, SIGNAL(needToShowSignal()), paramsetread, SLOT(updatePageShowSlot()));
        connect(paramsetread, SIGNAL(sendframeSignal_param_page()), this, SLOT(slot_netwriteData()));

    }
    else if(action == actionOopOperation)
    {
        oop_operation = new oopOperation();
        oop_operation->setWindowModality(Qt::ApplicationModal);
        oop_operation->setAttribute(Qt::WA_ShowModal,true);
        oop_operation->show();
        connect(oop_operation, SIGNAL(sendframeSignal_param_page()), this, SLOT(on_serial_write_slot()));
        connect(this, SIGNAL(needToShowSignal()), oop_operation, SLOT(updatePageShowSlot()));
//        connect(oop_operation, SIGNAL(sendframeSignal_param_page()), this, SLOT(slot_netwriteData()));
    }
    else if( action ==action645Operation)
    {
        operation_645 = new protocol645();
//        operation_645->setWindowModality(Qt::ApplicationModal);
//        operation_645->setAttribute(Qt::WA_ShowModal,true);
        operation_645->setWindowFlags(operation_645->windowFlags() | Qt::Tool);
        operation_645->show();
        thread645.start();
        connect(&thread645, SIGNAL(sendframeSignal_param_page()), this, SLOT(on_serial_write_slot()));
        connect(this, SIGNAL(needToShowSignal()), &thread645, SLOT(updatePageShowSlot()));
         connect(&thread645, SIGNAL(sendShowContent(QString)), operation_645, SLOT( logSlot(QString)));
    }
    else
    {
         qDebug()<<"other";
//		 serialTxRx = new monitorSerialRxTx();
//		 serialTxRx->show();
//		 gSystemInfo.monitor_serial_tx_rx = true;
    }
}
void MainWindow::open_network_slot()  // 打开网络的槽
{
    connectIsValid =TRUE;
    socketConnectState->setText(tr("连接中"));
    tcpClient->connectToHost(network_ip->text(), network_port->text().toInt(),QIODevice::ReadWrite,QAbstractSocket::IPv4Protocol);
   // ui->btnOpen->setEnabled(false);
    if( !tcpClient->waitForConnected(500) ) //只等待4秒
    {
        tcpClient->disconnectFromHost();
//        ui->btnOpen->setText(tr("连接"));
//        ui->btnOpen->setEnabled(true);
//           QMessageBox::warning(this,
//            tr("注意！"),
//            tcpClient->errorString(),
//            QMessageBox::Ok ,
//            QMessageBox::Ok) ;
    }
}
void MainWindow::close_network_slot()  //关闭网络的槽
{
    socketConnectState->setText(tr("连接关闭"));
    connectIsValid=FALSE;
    tcpClient->close();
    network_connect ->setEnabled(TRUE);
    network_close  ->setEnabled(FALSE);
}
void  MainWindow::slot_netreadData()
{
    serialIntCnt =0;
    QByteArray res = tcpClient->readAll();
    char *buf = res.data();
    int len = res.size();
    mutexUart.lock();
    for(int idx=0;idx <len;idx++)
    {
        if(serial_channel.receive_len_raw >=RECE_FRAME_LEN)
        {
            mutexUart.unlock();
            return;
        }
        serial_channel.recevice_frame[serial_channel.receive_len_raw] = *(buf+idx);
       serial_channel.receive_len_raw++;

    }
    mutexUart.unlock();
}
void  MainWindow::slot_connected()
{
    socketConnectState->setText(tr("连接成功"));
    qDebug()<<"client is  connected";
    network_connect ->setEnabled(FALSE);
    network_close  ->setEnabled(TRUE);
}
void  MainWindow::slot_disconnected()
{
    socketConnectState->setText(tr("连接断开"));
    qDebug()<<"client is  disconnected";
    network_connect ->setEnabled(TRUE);
    network_close  ->setEnabled(FALSE);
}
void MainWindow::slot_netwriteData()  //网口写槽。
{
    if(tcpClient->isOpen())
    {
        qDebug()<<"come here2 ";
        QByteArray ba = QByteArray::fromRawData( serial_channel.response_frame, serial_channel.response_len);

        {
            tcpClient->write(ba);
        }
        QDateTime current_date_time =QDateTime::currentDateTime();
        QString current_date =current_date_time.toString("yyyy.MM.dd hh:mm:ss.zzz");
        QString  trans;
        trans.clear();
        trans.append(current_date);
        trans.append(" ==>");
        for(int i=0;i<serial_channel.response_len;i++)
        {
          //  trans.append(QString::number(static_cast<uchar>(res[i]), 16));
            trans.append(QString("%1").arg(static_cast<uchar>(serial_channel.response_frame[i]), 2, 16, QLatin1Char('0')));
            trans.append(' ');
        }
         textshow->append(trans);
    }
}
void MainWindow::menu_add()
{
    //添加menu
    pMenuBar = menuBar();
    menuFile = pMenuBar->addMenu(tr("文件"));
    menuOperation= pMenuBar->addMenu(tr("控制"));
    menuHelp= pMenuBar->addMenu(tr("帮助"));
    menuFile->addAction(tr("打开"));
    menuFile->addAction(tr("保存"));
    menuFile->addSeparator();    // 插入分隔符
    menuFile->addAction(tr("退出"));
    action_network = menuOperation->addAction(tr("打开网络操作"));
    menuOperation->addAction(tr("串口收发记录"));
    menuOperation->addSeparator();// 插入分隔符
   action_sendTest =  menuOperation->addAction(tr("发送数据测试"));
   actionParamSetRead = menuOperation->addAction(tr("参数设置与查询"));
   actionOopOperation = menuOperation->addAction(tr("OOP协议操作"));
   action645Operation =  menuOperation->addAction(tr("645协议操作"));
    actionAbout = menuHelp->addAction(tr("关于"));

   connect(menuHelp,SIGNAL(triggered(QAction *)),this,SLOT(showAbout_slot(QAction *)));
   connect(menuOperation,SIGNAL(triggered(QAction *)),this,SLOT(show_Operator_slot(QAction *)));
}
